package com.big.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.big.app.config.RedisConfig;
import com.big.app.dao.FoodTypeDao;
import com.big.app.entity.FoodType;
import com.big.app.service.FoodTypeService;
import com.big.app.util.JedisUtils;
import com.big.app.vo.R;
import io.swagger.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodTypeServiceImpl implements FoodTypeService {
    @Autowired
    private FoodTypeDao dao;
    @Override
    public R queryTypes() {
        List<FoodType> foodTypes;
        if(JedisUtils.esists(RedisConfig.FOOD_TYPE)){
            //从缓存中查询
            foodTypes=JSON.parseArray(JedisUtils.getStr(RedisConfig.FOOD_TYPE),FoodType.class);
        }else{
            //不存在,查询数据库，同时同步更新到缓存中
            foodTypes = dao.selectList(null);
            JedisUtils.setStr(RedisConfig.FOOD_TYPE, JSON.toJSONString(foodTypes));
        }
        return new R(200,foodTypes);
    }
}
